package cn.stylefeng.guns.sys.modular.system.controller;
import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.core.log.LogObjectHolder;
import cn.stylefeng.guns.sys.core.util.FileUtils;
import cn.stylefeng.guns.sys.core.util.UUIDUtils;
import cn.stylefeng.guns.sys.modular.system.entity.BtyBxda;
import cn.stylefeng.guns.sys.modular.system.service.BtyBxdaService;
import cn.stylefeng.guns.sys.modular.system.warpper.NoticeWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 报销档案控制器
 * @author liuy
 * @Date
 */
@Controller
@RequestMapping("/btyBxda")
public class BtyBxdaController extends BaseController {

    private String PREFIX = "/modular/system/btyBxda/";
    @Resource
    private BtyBxdaService btyBxdaService;
    /**
     * 跳转到通知列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "btyBxda.html";
    }
    /**
     * 获取通知列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition,String ghs) {
        //获取用户的帐号
        String account= LoginContextHolder.getContext().getUser().getName();
        String username= LoginContextHolder.getContext().getUser().getAccount();
        Page<Map<String, Object>> list;
        if(account.equals("admin")|username.equals("chujunjun")){
            list = this.btyBxdaService.list(condition,null,ghs);
        }else {
            list = this.btyBxdaService.list(condition,account,ghs);
        }
        //Page<Map<String, Object>> list = this.btyBxdaService.list(condition);
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();

        return LayuiPageFactory.createPageInfo(wrap);
    }
    /**
     * 跳转到添加档案
     */
    @RequestMapping("/btyBxda_add")
    public String noticeAdd() {
        return PREFIX + "btyBxda_add.html";
    }
    /**
     * 跳转到修改档案
     */
    @RequestMapping("/btyBxda_update/{id}")
    public String noticeUpdate(@PathVariable Long id, Model model) {
        BtyBxda btyBxda = this.btyBxdaService.getById(id);
        model.addAllAttributes(BeanUtil.beanToMap(btyBxda));
        LogObjectHolder.me().set(btyBxda);
        return PREFIX + "btyBxda_edit.html";
    }
    /**
     * 跳转到批量修改会计
     */
    @RequestMapping("/btyBxda_updateKj/{id}")
    public String noticeUpdateKj(@PathVariable Long id, Model model) {
        BtyBxda btyBxda = this.btyBxdaService.getById(id);
        model.addAllAttributes(BeanUtil.beanToMap(btyBxda));
        LogObjectHolder.me().set(btyBxda);
        return PREFIX + "btyBxda_editKj.html";
    }
    /**
     * 修改档案
     * @author liuy
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(BtyBxda btyBxda) {
        if (ToolUtil.isOneEmpty(btyBxda,btyBxda.getId())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.btyBxdaService.updateById(btyBxda);
        return SUCCESS_TIP;
    }

    /**
     * 批量修改会计
     * @author liuy
     */
    @RequestMapping(value = "/updateKj")
    @ResponseBody
    public Object updateKj(BtyBxda btyBxda) {
        if (ToolUtil.isOneEmpty(btyBxda,btyBxda.getId())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        String kj=btyBxda.getKj();
        String dm=btyBxda.getDm();
        this.btyBxdaService.updatekj(kj,dm);
        return SUCCESS_TIP;
    }

    /**
     * 新增报销档案
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(BtyBxda btyBxda) {
        if (ToolUtil.isOneEmpty(btyBxda)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        SimpleDateFormat df=new SimpleDateFormat("yyyyMMddHHmmss");
        String bh = df.format(new Date());
        //替换大部分空白字符,不限于空格
        String yhk=btyBxda.getYhk().replaceAll("\\s*", "");
        btyBxda.setYhk(yhk);
        btyBxda.setBh(bh);
        btyBxda.setCreateUser(LoginContextHolder.getContext().getUserId());
        btyBxda.setCreateTime(new Date());
        this.btyBxdaService.save(btyBxda);
        return SUCCESS_TIP;
    }
    /**
     * 上传图片
     */
    @RequestMapping(method = RequestMethod.POST, path = "/upload")
    @ResponseBody
    public Map<String,Object> uploads(@RequestPart("file") MultipartFile file, String packName,@RequestParam String dm) {

        Map<String,Object> msg = new HashMap<>(16);
        try {
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //文件后缀
            String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            //图片名称为uuid+图片后缀防止冲突
            String fileName = dm+UUIDUtils.getUUID()+"."+suffix;
            String os = System.getProperty("os.name");
            //文件保存路径
            String filePath="";
           // filePath=ConstantsContext.getFileUploadPath();
            if(os.toLowerCase().startsWith("win")){
                //windows下的路径
                filePath ="D:/pictureUpload/project/";
            }else {
                //linux下的路径
                filePath="/root/pictureUpload/project/";
            }
            //写入图片
            Boolean writePictureflag = FileUtils.uploadFile(file.getBytes(),filePath,fileName);
            //上传成功后，将可以访问的完整路径返回
            String fullImgpath = "/pictureUpload/project/"+fileName;
            //String fullImgpath = filePath+fileName;
            Map<String,Object> data = new HashMap<>(16);
            //0表示成功，1失败
            msg.put("code",0);
            msg.put("msg","上传成功");
            msg.put("data",data);
            data.put("src", fullImgpath) ;
            data.put("title", StringUtils.substringAfterLast(fullImgpath,"/"));
        } catch (Exception e) {
            e.printStackTrace();
            msg.put("code",1);
            msg.put("msg","上传失败");
            throw new ServiceException(BizExceptionEnum.UPLOAD_ERROR);
        }
        return msg;
    }
    /**
     * 档案详情
     */
    @RequestMapping("/info")
    @ResponseBody
    public Object getThemeInfo(@RequestParam String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new RequestEmptyException();
        }
        BtyBxda btyBxda=this.btyBxdaService.getById(id);
        String account= LoginContextHolder.getContext().getUser().getAccount();
        if(account.equals("admin")|account.equals("chujunjun")){
            btyBxda.setRoleFlag(0);
        }else{
            btyBxda.setRoleFlag(1);
        }
        return btyBxda;
    }
    /**
     * 删除报销档案
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long id) {
        this.btyBxdaService.removeById(id);
        return SUCCESS_TIP;
    }
    /**
     * 查询店名
     */
    @RequestMapping(value = "/getDm")
    @ResponseBody
    public List<BtyBxda> getDm(){
        String account= LoginContextHolder.getContext().getUser().getName();
        List<BtyBxda> list;
//        if(account.equals("admin")){
//            list = this.btyBxdaService.dmList(null);
//        }else {
//            list =this.btyBxdaService.dmList(account);
//        }
        list =this.btyBxdaService.dmList(account);
        return list;
    }
}
